package com.wx.commons.result;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author: 王霄
 * @date: 2021/9/6 20:31
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class R<T> implements Serializable {

    private int code;
    private String message;
    private T data;

    public static <T> R<T> ok(T data) {
        return new R<>(RCode.SUCCESS.getCode(), RCode.SUCCESS.getMsg(), data);
    }

    public static <T> R<T> ok() {
        return ok(null);
    }

    public static <T> R<T> fail(T data) {
        return new R<>(RCode.FALL.getCode(), RCode.FALL.getMsg(), data);
    }

    public static <T> R<T> fail() {
        return new R<>(RCode.FALL.getCode(), RCode.FALL.getMsg(), null);
    }

    public static <T> R<T> fail(RCode resultEnum, T data) {
        return new R<>(resultEnum.getCode(), resultEnum.getMsg(), data);
    }

    public static <T> R<T> fail(RCode resultEnum) {
        return new R<>(resultEnum.getCode(), resultEnum.getMsg(), null);
    }

}
